package com.example.springboot.pattern.strategy;

public class ShoppingCart {
    private DiscountStrategy discountStrategy;
    private double total;

    public ShoppingCart(DiscountStrategy strategy) {
        this.discountStrategy = strategy;
    }

    public void setDiscountStrategy(DiscountStrategy strategy) {
        this.discountStrategy = strategy;
    }

    public void addItem(double price) {
        total += price;
    }

    public double checkout() {
        double discountedPrice = discountStrategy.calculateDiscount(total);
        // 结算后清零总金额
        total = 0;
        return discountedPrice;
    }
}
